<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterData && !filterLoader">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="javascript:;"
          :class="{ active: item.id === filterData.brands.selectedBrands }"
          v-for="item in filterData.brands"
          :key="item.id"
          @click="changeBrands(item.id)"
          >{{ item.name }}</a
        >
      </div>
    </div>
    <div class="item" v-for="prop in filterData.saleProperties" :key="prop.id">
      <div class="head">{{ prop.name }}</div>
      <div class="body">
        <a
          href="javascript:;"
          :class="{ active: item.id === prop.selectedAttr }"
          v-for="item in prop.properties"
          :key="item.id"
          @click="changeAttr(prop, item.id)"
          >{{ item.name }}</a
        >
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubFilter } from '@/api/category'
export default {
  name: 'SubFilter',
  setup(prop, { emit }) {
    const route = useRoute()
    const filterData = ref(null)
    const filterLoader = ref(true)
    const getFindSubFilter = () => {
      filterLoader.value = true
      // 获取筛选区数据
      findSubFilter(route.params.id).then(data => {
        // 每一组可选筛选条件缺少一个全部条件，处理数据加上全部
        // 品牌
        data.result.brands.selectedBrands = null
        data.result.brands.unshift({ id: null, name: '全部' })

        // 规格
        data.result.saleProperties.forEach(item => {
          item.selectedAttr = null
          item.properties.unshift({ id: null, name: '全部' })
        })
        filterData.value = data.result
        filterLoader.value = false
      })
    }
    // 监听二级类目id的变化
    watch(
      () => route.params.id,
      newVal => {
        // 判断是否在二级类目下
        if (newVal && `/category/sub/${newVal}` === route.path) {
          getFindSubFilter()
        }
      },
      { immediate: true }
    )
    // 获取筛选参数的函数
    const getFilterParams = () => {
      // 参考请求数据 {brandId:'',attrs:[{groupName:'',propertyName:'',}]}
      const obj = { brandId: null, attrs: [] }
      //品牌
      obj.brandId = filterData.value.brands.selectedBrands
      // 销售属性
      filterData.value.saleProperties.forEach(item => {
        if (item.selectedAttr != null) {
          const prop = item.properties.find(prop => prop.id === item.selectedAttr)
          obj.attrs.push({ groupName: item.name, propertyName: prop.name })
        }
      })
      if (obj.attrs.length === 0) obj.attrs = null
      return obj
    }
    // 改变品牌
    const changeBrands = id => {
      if (filterData.value.brands.selectedBrands === id) return
      filterData.value.brands.selectedBrands = id
      emit('ChangeFilter', getFilterParams())
    }
    // 改变属性
    const changeAttr = (prop, id) => {
      if (prop.selectedAttr === id) return
      prop.selectedAttr = id
      emit('ChangeFilter', getFilterParams())
    }
    return { filterData, filterLoader, changeBrands, changeAttr }
  }
}
</script>
<style scoped lang="less">
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
  .xtx-skeleton {
    padding: 10px 0;
  }
}
</style>
